package de.farbstreich.tozen2.server;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * @author martin
 * This class sends every 5 seconds a PING to a Hashmap of players
 *
 */
public class PingThread extends Thread {
	HashMap<PlayerThread, Long> timetable;

	public PingThread(HashMap<PlayerThread, Long> timetable) {
		this.timetable = timetable;
		this.start();
		
	}
	@Override
	public void run() {
		while(true){
			Long m = new Date().getTime();
			for(Map.Entry<PlayerThread, Long> e : timetable.entrySet()){
				if(e.getValue()+5000 < m){
					synchronized(this) {
						e.getKey().kill();
						System.out.println("Lost connection to " + e.getKey().getPlayerName() + " (id:"+e.getKey().getPlayerId()+")");
						timetable.remove(e.getKey());
					}
					
				}
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
